<?php
require_once "common.php";
class article{
	private $aid="";//文章id
	private $title="";//文章标题
	private $content="";//文章内容
	private $pid="";//文章所属栏目id
	private $time="";//文章上传时间
	private $keyword="";//文章关键字
	private $describe="";//文章简介
    private $recommend=""; //热门推荐
    private $slide_show="";//首页幻灯
    //构造函数
    public function __construct($aid,$title,$content,$pid,$time,$keyword,$describe,$recommend,$slide_show){
		$this->aid=$aid;
		$this->title=$title;
		$this->content=$content;
		$this->pid=$pid;
		$this->time=$time;
		$this->keyword=$keyword;
        $this->describe=$describe;
        $this->recommend=$recommend;
        $this->slide_show=$slide_show;
	}
	//自动载入相应的类
	function __autoload(){
		require_once("class_".ucfirst($className).".php");
	}
	//添加文章函数
	public function addArticle($article){
        if(is_array($article)){
                //使用foreach函数分别拼接出名称和对应的值
                foreach ($article as $key=>$value){
                        $colName=$colName."`".$key."`,";
                        $valName=$valName."'".$value."',";
                    }
                    //使用字符处理函数去除掉后面的逗号
                    $colName=rtrim($colName,",");
                    $valName=rtrim($valName,",");
                    $valName=substr_replace($valName,'NULL',0,2);
                    $sql="INSERT INTO `test`.`article` (".$colName.") VALUES (".$valName.")";
                    //echo $colName."<br/>";
                    //echo $valName."<br/>";
                    //echo $sql;
                    $mysql= new mysql();
                    $res=$mysql->query($sql);
                    var_dump($res);
                    return $res;
            }else{ return false; }
        /*
		$sql="INSERT INTO article (aid,title,content,pid,time,keyword,describe,recommend,slide_show) VALUES ('','".$this->title."','".$this->content."','".$this->pid."','".$this->time."','".$this->keyword."','".$this->describe."','".$this->recommend."','".$this->silde_show."')";
		$mysql=new mysql();
		$res=$mysql->query($sql);
		return $res;
        */
	}
	//修改文章函数
	public function editArticle($article){
        if(is_array($article)){
                foreach($article as $key=>$value){
                    $string=$string."`".$key."`"."='".$value."',";
                }
                $string=rtrim($string,",");
                $sql="UPDATE test.article SET ".$string." WHERE aid=".$this->aid;
                echo $sql;
                $mysql= new mysql();
                $res=$mysql->query($sql);
                return $res;
            }else{ return false; }
                
		/*
        $sql="UPDATE test.article SET title='".$article[title]."',content='".$article[content]."',keyword='".$article[keyword]."' WHERE aid='".$this->aid."'";
		$mysql= new mysql();
		$res=$mysql->query($sql);
		return $res;
        */
	}
	//返回文章列表中按时间排序在前十的文章
	public function showArticle(){
		$sql="SELECT * FROM article ORDER BY time DESC LIMIT 0,10";
		$mysql= new mysql();
		$res=$mysql->query($sql);
		while($array[]=mysql_fetch_array($res));
		return $array;
	}
    //返回热门文章
    public function hotNews(){
        $sql="SELECT * FROM article WHERE recommend=1 ORDER BY time DESC LIMIT 0,10";
        $mysql= new mysql();
        $res=$mysql->query($sql);
        while($array[]=mysql_fetch_array($res));
        return $array;
        }
	//返回指定的一篇文章
	public function returnArticle(){
		$sql="SELECT * FROM article WHERE aid='".$this->aid."'";
		$mysql=new mysql();
		$res=$mysql->query($sql);
		$array=mysql_fetch_array($res);
		return $array;

	}
	//删除指定文章
	public function delArticle(){
		$sql="DELETE FROM article WHERE aid='".$this->aid."'";
		$mysql= new mysql();
		$res=$mysql->query($sql);
		return $res;
	}
    //返回分页对应的文章
    //$offser偏移量$rows起始值
    public function subPage($rows,$offset,$pid){
        $sql="SELECT * FROM article WHRER pid=".$pid." LIMIT ".$rows.",".$offset;
        $mysql=new mysql();
        $res=$mysql->query($sql);
        while($array[]=mysql_fetch_array($res));
        return $res;
        }
    //返回首页幻灯文章
    public function slideShow(){
            $sql="SELECT * FROM article WHERE slide_show=1";
            $mysql=new mysql();
            $res=$mysql->query($sql);
            while($array[]=mysql_fetch_array($res));
            return $array;
        }
	//根据pid返回指定栏目文章
	public function partList(){
		$sql="SELECT * FROM article WHERE pid='".$this->pid."' ORDER BY time DESC";
		$mysql= new mysql();
		$res=$mysql->query($sql);
		while($array[]=mysql_fetch_array($res));
		return $array;
	}
	//文章输出时使用的正则输出文章中的图片
	public function pregMatch($content){
		$partten='/<img.*\/>/';
		$res=preg_match($partten,$content,$matches);
		if($res!=1){
			$matches[0]='<img src="./slider/pic1.jpg">';
			return $matches;
		}
		return $matches;
	}
    //正则输出文章中的图片地址
    public function getPic($content){
        $partten='/src=["]?([^"]*)["]?/';
        $res=preg_match($partten,$content,$matches);
        if($res!=1){
            $matches[0]='src="./slider/pic1.jpg"';
            return $matches;
            
            }
        //输出是有两个结果，$matches[0]有src=和",$matches[1]只有图片地址
        return $matches;
        }
	//返回文章表中指定的一个值
	public  function getCol($col,$aid){
			if($col==""&&$aid==""){
			return false;
		}
		$sql="SELECT ".$col." FROM article WHERE aid='".$aid."'";	
		//echo $sql;
		$mysql=new mysql();
		//$val=$mysql->query($sql);
		//mysql_fetch_row只能返回数组
		$val=mysql_fetch_row($mysql->query($sql));
		$res=$val[0];
		return $res;	
	}
}
//@$article=new article();
//$array=array("",2,3);
//$article->editArticle($array);
//$res=$article->getCol('content',12);
//var_dump($res);
//$test=$article->pregMatch($res);
//print_r($test);
//$matches=$article->getPic($res);
//print_r($matches);
?>
